package com.xy.usermanagement.readermanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.xy.model.PageList;
import com.xy.model.Readers;
import com.xy.model.UserStatus;
import com.xy.usermanagement.accountmanagement.mapper.UserStatusMapper;
import com.xy.usermanagement.readermanagement.mapper.ReadersMapper;
import com.xy.usermanagement.readermanagement.service.IReaderManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ReaderManagementServiceImpl implements IReaderManagementService {
		@Autowired
		private ReadersMapper mapper;
		@Autowired
		private UserStatusMapper userStatusMapper;

		@Override
		public PageList<Readers> queryForPage(Integer pageNum, Integer pageSize) {
				log.info("ReadersManagementService queryForPage pageNum = " + pageNum);
				log.info("ReadersManagementService queryForPage pageSize = " + pageSize);
				if (pageNum == null) {
						pageNum = 1;
				}
				if (pageSize == null) {
						pageSize = 20;
				}
				PageHelper.startPage(pageNum, pageSize);
				List<Readers> list = null;
				try {
						list = mapper.selectAll();
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
				}
				log.info("ReadersManagementService queryForPage=>list = " + list);
				return new PageList<>(list);
		}

		@Override
		public Readers getById(String name) {
				log.info("ReadersManagementService getById=>name = " + name);
				return mapper.selectByPrimaryKey(name);
		}

		@Override
		public Integer add(Readers readers) {
				log.info("ReadersManagementService queryForPage=>author = " + readers);
				int i = mapper.insert(readers);
				UserStatus userStatus = new UserStatus();
				userStatus.setNickName(readers.getNickName());
				userStatus.setStatus(1);
				userStatusMapper.insert(userStatus);
				return i;
		}

		@Override
		public void update(Readers readers) {
				log.info("ReadersManagementService update=>author = " + readers);
				mapper.updateByPrimaryKey(readers);
		}
}
